<?php
class YumWorkTime extends YumActiveRecord
{
    public static function model($className = __CLASS__)
	{
		return parent::model($className);
	}

    public function tableName()
    {
        return "work_times";
    }

    public function rules()
    {
        return array(
            array('post_id', 'required'),
        );
    }

    public function relations()
    {
        return array(
            'post' => array(self::BELONGS_TO, 'Post', 'post_id'),
            'startTime' => array(self::BELONGS_TO, 'YumTime', 'start_time_id'),
            'endTime' => array(self::BELONGS_TO, 'YumTime', 'end_time_id'),
            'type' => array(self::BELONGS_TO, 'YumTimeType', 'type_id'),
            'weekDay' => array(self::BELONGS_TO, 'YumWeekDay', 'week_day_id'),
        );
    }


    public function loadItems($post_id=0)
    {
        $dbItems = self::loadSet($post_id);
        $items = array();
        foreach ($dbItems as $dbItem) {
            $items[$dbItem->id] = Yum::t($dbItem->name);
        }
        return $items;
    }

    public static function loadSet($post_id=0) {
        $set = YumWorkTime::model()->findAll('post_id=:postId', array(':postId' => $post_id));

        if (sizeof($set) == 0 ) {
            $weekDays = YumWeekDay::model()->findAll();
            foreach($weekDays as $weekDay) {
                $time = new YumWorkTime;
                $time->post_id = $post_id;
                $time->week_day_id = $weekDay->id;
                $time->start_time_id = 1;
                $time->end_time_id = 3;
                $time->type_id = 1;
                $time->save();
                $set[] = $time;
            }
        }
        return $set;
    }

    public function isTime() {
        return $this->type_id == 1;
    }
    public function isFree() {
        return $this->type_id == 2;
    }
    public function isAll() {
        return $this->type_id == 3;
    }
}
?>